package Manager;


import Commons.Address;
import Commons.Indirizzi;
import Commons.TaskDescriptor;
import Commons.Utility;
import Commons.WorkerDescriptor;
import java.net.InetAddress;
import java.net.UnknownHostException;


/**Questa è la classe che implementa la comunicazione a basso livello con il TQueueManager in modalità UDP PUSH.
 *
 *
 * @see TQueueManager
 * @author Team5
 */
public class TQueueManagerProxy_UDP_PUSH extends TQueueManagerProxy {
    private int cont=0;
    private int chiu=0;
    private Address addr;
    /**
     * Indirizzo del Server
     */
    private InetAddress address;
    /**
     * Porta del WFQueueManager
     */
    private int portWf;

    /**
     * Porta del FileRecieveManager
     */
    private int portFile;


    private InetAddress addressDelWorker;



      /**Costruttore di default. Associa come predefiniti i parametri di invio definiti nella classe Commons.Indirizzi.
       * @see Commons.Indirizzi
     * @throws UnknownHostException
     */
    public TQueueManagerProxy_UDP_PUSH() throws UnknownHostException{
    }

    public Address getAddr(){
        return this.addr;
    }

    public void setAddr(Address val){
        addr=val;
    }

    @Override
    public int enqueue(TaskDescriptor ts) {
       
        try{
        System.out.println("TQMPUP: Faccio l'enqueue sul TQueueManager");
        Integer i=-1;
       

        Utility u=new Utility();
        u.invioOggettoSerializzatoUDP(ts,InetAddress.getByName(addr.getIP()),Indirizzi.portaRicezioneTDTQUeueManager);
        System.out.println("%%%%% TQMPUDPPUSH ---> "+addr.getPort()+" "+addr.getIP());
        u.chiudiSocket();
        System.out.println("%%%%% TQMPUDPPUSH u close --->");

        Utility u4=new Utility();
        System.out.println("cont="+cont);
        cont++;
        System.out.println("======================================contatore="+cont);
        u4.inizializzaServerRicezioneUDP(Indirizzi.portaRicezioneTD);//15500 s
        System.out.println("%%%%% TQMPUDPPUSH <----15500");
        i=  (Integer) u4.riceviOggettoSerializzatoUDP();
        u4.chiudiSocket();
        chiu++;
        System.out.println("======================================chiusure="+chiu);
         System.out.println("%%%%% TQMPUDPPUSH u4 close <---");
        return i;
        }catch(Exception e){
            return -1;
        }
       
    }

    public TaskDescriptor dequeue() {
        throw new UnsupportedOperationException("Not supported yet.");
    }


    public int updateWorker(int index, WorkerDescriptor wd) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public int addworker(WorkerDescriptor td) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
